import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/Login.vue'

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
	children:[
		{path: '/user',name: 'User',component: () => import('../views/User.vue')},
		{path: '/main',name: 'Main',component: () => import('../views/Main.vue')},
		{path: '/food',name: 'Food',component: () => import('../views/Food.vue')},
		{path: '/online',name: 'Online',component: () => import('../views/Online.vue')}
	]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
    //to 要访问的路径
    //from 代表从哪个路径跳转而来
    // next 是函数，表示放行
    // next() 放行
    // next('/*') 强制跳转
    const user = window.sessionStorage.getItem('user')
    if (to.path === '/') {
        return next();
    }
    if (!user) {
        return next('/')
    }
    if (to.path === '/' && user) {
        return next('/home')
    }
    next()
})

export default router
